package com.totcy.atom.view.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.animation.DecelerateInterpolator;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.totcy.atom.CommonValues;
import com.totcy.atom.R;
import com.totcy.atom.adapter.XImageListAdapter;
import com.totcy.atom.bean.ImagesBean;
import com.totcy.atom.presenter.ImageListPresenter;
import com.totcy.atom.presenter.contract.ImageListContract;
import com.totcy.baselibrary.adapter.BaseLoadMoreAdapter;
import com.totcy.baselibrary.adapter.loadmore.OnLoadMoreListener;
import com.totcy.baselibrary.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * description: $xxiao 图片列表
 * author: Totcy
 * date: 2017/11/4 21:51
 * update: 2017/11/4
 */
public class ImageListActivity extends BaseFullActivity<ImageListContract.View,ImageListPresenter> implements ImageListContract.View, OnLoadMoreListener, OnRefreshListener, BaseLoadMoreAdapter.OnItemClickListener {


    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refresh)
    SmartRefreshLayout refresh;

    private String type = CommonValues.XINGGAN;
    public static final int PAGE_START = 1;
    private int pageNum = PAGE_START; //页面从第二页开始 第一页url就不用拼接 page
    private List<ImagesBean> list = new ArrayList<>();
    private XImageListAdapter adapter;
    private int typePosition;


    /**
     * @return 获取标题名
     */
    @Override
    public String getTitleName() {
        return getIntent().getStringExtra("typeName");
    }

    /**
     * 布局Id 解析添加到 layout/layout_full_base_activity.xml中
     *
     * @return 子activity layout id
     */
    @Override
    public int getLayoutId() {
        return R.layout.activity_image_list;
    }

    /**
     * 初始化View
     */
    @Override
    public void initView() {
        type =  getIntent().getStringExtra("type");
        typePosition =  getIntent().getIntExtra("typePosition",0);

        LinearLayoutManager lm = new LinearLayoutManager(this);
        lm.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(lm);
        adapter = new XImageListAdapter(this, list);
        recyclerView.setAdapter(adapter);

        adapter.setOnLoadMoreListener(recyclerView, this);
        adapter.setOnItemClickListener(this);
        //设置回弹动画插值器 减速效果
        refresh.setReboundInterpolator(new DecelerateInterpolator());
        refresh.setOnRefreshListener(this);
        //自动刷新
        refresh.autoRefresh(200);
    }

    @Override
    public void onItemClick(int position) {
        Bundle bundle = new Bundle();
        bundle.putString("title", list.get(position).getDescription());
        bundle.putString("linkUrl", list.get(position).getImageLink());
        openActivity(ImageGridActivity.class, bundle);
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        pageNum = PAGE_START;
        getPresenter().getImageListData(type,typePosition, pageNum);
    }

    /**
     * when recyclerview scroll to bottom callback
     */
    @Override
    public void onLoadMore() {
        pageNum++;
        getPresenter().getImageListData(type,typePosition, pageNum);
    }

    /**
     * when load fail click to callback
     */
    @Override
    public void onClickLoadMore() {
        getPresenter().getImageListData(type,typePosition, pageNum);
    }

    /**
     * xxxiao 分类数据
     *
     * @param images
     */
    @Override
    public void onImageListData(List<ImagesBean> images) {
        refresh.finishRefresh(true);

        if (pageNum == PAGE_START) {
            list.clear();
        }
        list.addAll(images);
        adapter.notifyDataSetChanged();
        adapter.setLoadMoreFinish(images.size() >= 10);
    }

    /**
     * 这里获取数据失败
     */
    @Override
    public void onDataFail() {
        refresh.finishRefresh(false);
        ToastUtil.showToast(this,"哦，看不了！！！");
    }


}
